<?php

if (!defined('EXPONENT')) exit('');

exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_ACTION);

$template = new template('weblogmodule','Default',$loc);

$config = $db->selectObject('weblogmodule_config',"location_data='".serialize($loc)."'");
if ($config == null) {
	$config->allow_comments = 1;
	$config->items_per_page = 10;
}

if (isset($_GET['single'])) {
	$config->items_per_page = 1;
}

// get module title
$module_container=$db->selectObject('container',"internal = '".serialize($loc)."'");
		
$where = "location_data='".serialize($loc)."' AND (is_draft = 0 OR poster = ".($user ? $user->id : -1).")";
// page split
			// cal how many posts
			$count_post = $db->countObjects('weblog_post',$where );
			$page_count=$count_post/$config->items_per_page;
			if ($count_post % $config->items_per_page != 0)
			  $page_count++;
			// build page array
			$pages = array();
			for ($i=1;$i<=$page_count;$i++)
				$pages[$i-1] = $i;
				
if (!exponent_permissions_check('view_private',$loc)) $where .= ' AND is_private = 0';;

$total = $db->countObjects('weblog_post',$where);
$posts = $db->selectObjects('weblog_post',$where . ' ORDER BY posted DESC '.$db->limit($config->items_per_page,($_GET['page']*$config->items_per_page)));
if (!defined('SYS_SORTING')) require_once(BASE.'subsystems/sorting.php');
for ($i = 0; $i < count($posts); $i++) {
	$ploc = exponent_core_makeLocation($loc->mod,$loc->src,$posts[$i]->id);
	
	$posts[$i]->permissions = array(
		'administrate'=>exponent_permissions_check('administrate',$ploc),
		'edit'=>exponent_permissions_check('edit',$ploc),
		'delete'=>exponent_permissions_check('delete',$ploc),
		'comment'=>exponent_permissions_check('comment',$ploc),
		'edit_comments'=>exponent_permissions_check('edit_comments',$ploc),
		'delete_comments'=>exponent_permissions_check('delete_comments',$ploc),
		'view_private'=>exponent_permissions_check('view_private',$ploc),
	);
	// immanuel add this code for split the body to 2
				// remove html tag
				$temp=strip_tags($posts[$i]->body);
				// get first 100 chars
				$myreturn=substr($temp,0,300);
				$myindex=300;
				$mytest=substr($temp,$myindex,1);
				while (($mytest != ".") && ($myindex<strlen($temp)))
				{
					$myreturn=$myreturn . $mytest;
					$myindex++;
					$mytest=substr($temp,$myindex,1);
				}
				$myreturn.=".";
				$posts[$i]->body=$myreturn;
	
}
usort($posts,'exponent_sorting_byPostedDescending');
$template->assign('posts',$posts);
$template->assign('pages',$pages);
$template->assign('cur_page',$_GET['page']);
$template->assign('page_count',$page_count);
$template->assign('total_posts',$total);
$template->assign('shownext',(($_GET['page']+1)*$config->items_per_page) < $total);
$template->assign('page',$_GET['page']);
$template->assign('moduletitle',$module_container->title);

$template->register_permissions(
	array('administrate','configure','post','edit','delete','comment','edit_comments','delete_comments','view_private'),
	$loc);
$template->assign('config',$config);

$template->output();
